﻿//===============================================================================
//	user_define.h
//	自定义的一些可用数据
//===============================================================================

#pragma once

#include <cstdio>
#include "log.h"
//-------------------------------------------------------------------------------
#if defined (_MSC_VER) || defined (__CYGWIN__)
	#ifdef DLL_EXPORT	//	生成DLL时需定义
		#ifdef __GUNC__
			#define DLL_PUBLIC __attribute__((dllexport))
		#else // __GUNC__
			#define DLL_PUBLIC __declspec(dllexport)
		#endif // __GUNC__
	#else
		#ifdef __GUNC__
		#define DLL_PUBLIC __attribute__((dllimport))
		#else // __GUNC__
		#define DLL_PUBLIC __declspec(dllimport)
		#endif // __GUNC__
	#endif // _EXPORTING

	#define DLL_HIDDEN
#else
	#if __GUNC__ >= 4
		#define DLL_PUBLIC __attribute__(visibility("default"))
		#define DLL_HIDDEN __attribute__(visibility("hidden"))
	#else // __GUNC__ >= 4
		#define DLL_PUBLIC
		#define DLL_HIDDEN
	#endif // __GUNC__ >= 4
#endif // defined _WIN32 || defined __CYGWIN__

//-------------------------------------------------------------------------------
#ifdef __GNUC__
	#define DEPRECATED __attribute__ ((deprecated))
#elif defined(_MSC_VER)
	#define DEPRECATED __declspec(deprecated)
#else
	#define DEPRECATED
	#pragma message("DEPRECATED is not defined for this compiler")
#endif

//-------------------------------------------------------------------------------
#if defined(_MSC_VER)
#define _safe_guard_begin				{ try { 
#define _safe_guard_end					} catch(...) { _safe_check( false ); } }
#define _safe_guard_end_user_define		} catch(...) {
#define _safe_guard_end_user_define_end	} }
#define snprintf			_snprintf 
#define vsnprintf			_vsnprintf
#elif defined(__GNUC__)
#define _safe_guard_begin				{ try { 
#define _safe_guard_end					} catch(...) { _safe_check( false ); } }
#define _safe_guard_end_user_define		} catch(...) {
#define _safe_guard_end_user_define_end	} }
#define snprintf			snprintf
#define vsnprintf			vsnprintf
#endif

//-------------------------------------------------------------------------------
#define _safe_check_ex(assertion, err_str) if ( ! (assertion) ) \
{ LOG_ERROR("default", "Alart!!!:" );\
	LOG_ERROR("default", "%s", err_str); throw (1); }

#define _safe_check(assertion) if ( ! (assertion) ) \
{	LOG_ERROR("default", "Alart!!!" );\
	throw (1);	}

//-------------------------------------------------------------------------------
#define _safe_delete(p)       { if(p) { delete (p);     (p) = nullptr; } }
#define _safe_delete_array(p) { if(p) { delete[] (p);   (p) = nullptr; } }

//-------------------------------------------------------------------------------
#define _mem_zero(p, s)	{ memset( (p), 0, (s) ); }

//-------------------------------------------------------------------------------
#define _array_size(array) (sizeof(array) / sizeof(array[0]))

//-------------------------------------------------------------------------------
#define access_variable(var_type, func_name, var_name)	\
var_type Get##func_name(void) const { return var_name;}	\
void Set##func_name(var_type val) { var_name = val; }

enum trans_type
{
	e_client_to_server = 1,
	e_server_to_client, //下行到客户端
	e_server_to_server,  //服务器间传递消息
	e_all_to_server,    //
	e_server_broadcast, //广播
};
